home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / python / xpcom / shutdown.py < prev    next >
Encoding:
Python Source  |  2005-12-19  |  2.8 KB  |  75 lines

  1. # Utilities for registering functions to be called at xpcom shutdown.
  2. #
  3. # Pass xpcom.shutdown.register a function (and optionally args) that should
  4. # be called as xpcom shutsdown.  Relies on xpcom itself sending the
  5. # standard shutdown notification.
  6.  
  7. # ***** BEGIN LICENSE BLOCK *****
  8. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  9. #
  10. # The contents of this file are subject to the Mozilla Public License Version
  11. # 1.1 (the "License"); you may not use this file except in compliance with
  12. # the License. You may obtain a copy of the License at
  13. # http://www.mozilla.org/MPL/
  14. #
  15. # Software distributed under the License is distributed on an "AS IS" basis,
  16. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  17. # for the specific language governing rights and limitations under the
  18. # License.
  19. #
  20. # The Original Code is the Python XPCOM language bindings.
  21. #
  22. # The Initial Developer of the Original Code is Mark Hammond.
  23. # Portions created by the Initial Developer are Copyright (C) 2000
  24. # the Initial Developer. All Rights Reserved.
  25. #
  26. # Contributor(s):
  27. #
  28. # Alternatively, the contents of this file may be used under the terms of
  29. # either the GNU General Public License Version 2 or later (the "GPL"), or
  30. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31. # in which case the provisions of the GPL or the LGPL are applicable instead
  32. # of those above. If you wish to allow use of your version of this file only
  33. # under the terms of either the GPL or the LGPL, and not to allow others to
  34. # use your version of this file under the terms of the MPL, indicate your
  35. # decision by deleting the provisions above and replace them with the notice
  36. # and other provisions required by the GPL or the LGPL. If you do not delete
  37. # the provisions above, a recipient may use your version of this file under
  38. # the terms of any one of the MPL, the GPL or the LGPL.
  39. #
  40. # ***** END LICENSE BLOCK *****
  41. #
  42.  
  43. import xpcom.server
  44. from xpcom import _xpcom
  45. from xpcom.components import interfaces
  46.  
  47. import logging
  48.  
  49. _handlers = []
  50.  
  51. class _ShutdownObserver:
  52.     _com_interfaces_ = interfaces.nsIObserver
  53.     def observe(self, service, topic, extra):
  54.         logger = logging.getLogger('xpcom')
  55.         while _handlers:
  56.             func, args, kw = _handlers.pop()
  57.             try:
  58.                 logger.debug("Calling shutdown handler '%s'(*%s, **%s)",
  59.                              func, args, kw)
  60.                 func(*args, **kw)
  61.             except:
  62.                 logger.exception("Shutdown handler '%s' failed", func)
  63.  
  64. def register(func, *args, **kw):
  65.     _handlers.append( (func, args, kw) )
  66.  
  67. # Register
  68. svc = _xpcom.GetServiceManager().getServiceByContractID(
  69.                                     "@mozilla.org/observer-service;1",
  70.                                     interfaces.nsIObserverService)
  71.  
  72. svc.addObserver(_ShutdownObserver(), "xpcom-shutdown", 0)
  73.  
  74. del svc, _ShutdownObserver
  75.